#ifndef __BUOLA_SCENE_CMESH_H__
#define __BUOLA_SCENE_CMESH_H__

#include <buola/scene.h>
#include <buola/algorithm/container.h>
#include <buola/io/curi.h>
#include <buola/image/format.h>
#include <buola/gl/ctexture.h>

#include <buola/scene/cvertexdata.h>
#include <buola/scene/cindexdata.h>

namespace buola { namespace scene {

DECLARE_NAME(RestartPrimitive);
    
class CMesh : public UShared
{
public:
    struct SVertex
    {
        SVertex(int pPos,int pNormal,int pTex=-1)
            :   mPos(pPos)
            ,   mNormal(pNormal)
            ,   mTex(pTex)
        {}
        
        SVertex(NRestartPrimitive)
            :   mPos(-1)
        {}
        
        int mPos;
        int mNormal;
        int mTex;
    };
    
    struct SSubMesh
    {
        std::string mName;
        std::string mMaterial;

        EPrimitive mPrimitive;
        ptrdiff_t mFirstIndex;    //!< for rendering using mIndexData
        int mIndexCount;    //!< for rendering using mIndexData
    };

public:
    CMesh();
    ~CMesh();

    CVertexData &VertexData()           {   return mVertexData;     }
    CIndexData &IndexData()             {   return mIndexData;      }
    
    void AddSubMesh(const std::string &pName,EPrimitive pPrimitive,int pFirstIndex,int pIndexCount);
    
    void Render(const std::string &pSubMesh);
    void Render();
    void CollectPolygons(std::vector<mat::CVec3d> &pPoints,std::vector<mat::CVec3d> &pNormals,
                         std::vector<int> &pFaces,const mat::CMat34d &pTransform);
    
public:
    static PMesh Import(const io::CURI &pURI,double pScale=1.0);

private:
    std::vector<SSubMesh> mSubMeshes;

    CVertexData mVertexData;
    CIndexData mIndexData;
};

/*namespace scene*/ } /*namespace buola*/ }

#endif
